package org.mzy.pet.controller;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.mzy.basic.util.AjaxResult;
import org.mzy.basic.util.LoginContext;
import org.mzy.basic.util.PageList;
import org.mzy.org.domain.Employee;
import org.mzy.org.service.IEmployeeService;
import org.mzy.pet.domain.SearchMasterMsg;
import org.mzy.pet.domain.SearchMasterMsgAuditLog;
import org.mzy.pet.dto.AcceptDto;
import org.mzy.pet.query.SearchMasterMsgQuery;
import org.mzy.pet.service.ISearchMasterMsgService;
import org.mzy.user.domain.Logininfo;
import org.mzy.user.domain.User;
import org.mzy.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    public IUserService userService;

    @Autowired
    public IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     *
     * @param searchMasterMsg 传递的实体
     * @return Result转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id") Long id) {
        return searchMasterMsgService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<SearchMasterMsg> list() {

        return searchMasterMsgService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryByPage(query);
    }

    /*发布寻主信息*/
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        // 获取前端用户信息
        Logininfo logininfo = LoginContext.getLoginUser(request);
        // 通过登录人id找到用户信息
        User user = userService.findByLogininfoId(logininfo.getId());
        // 将用户id赋值给寻主信息的用户id
        searchMasterMsg.setUserId(user.getId());
        searchMasterMsgService.publish(searchMasterMsg);
        return AjaxResult.me().success();
    }

    //================消息列表====================//
    // 待审核
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query) {
        // 待审核状态为0
        query.setState(0);
        return searchMasterMsgService.queryPage(query);
    }

    // 待处理
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        // 待处理状态为1，且有店铺id
        query.setState(1);
        // 获取当前登录人信息
        Logininfo loginUser = LoginContext.getLoginUser(request);
        // 通过登录人的id查询员工信息
        Employee employee = employeeService.selectByLogininfoId(loginUser.getId());
        // 将员工的店铺id赋值给query的店铺id
        query.setShopId(employee.getShopId());
        return searchMasterMsgService.queryPage(query);
    }

    // 寻主池
    @PostMapping("/toSearchMasterPool")
    public PageList<SearchMasterMsg> toSearchMasterPool(@RequestBody SearchMasterMsgQuery query) {
        // 寻主池状态为1
        query.setState(1);
        return searchMasterMsgService.queryPage(query);
    }

    // 已完成
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        // 获取当前登录人信息
        Logininfo loginUser = LoginContext.getLoginUser(request);
        // 通过登录人的id查询员工信息
        Employee employee = employeeService.selectByLogininfoId(loginUser.getId());
        // 如果shopId不为null，就设置shopId
        if (employee.getShopId() != null) {
            query.setShopId(employee.getShopId());
        }
        // 已完成状态为3
        query.setState(3);
        return searchMasterMsgService.queryPage(query);
    }

    // 用户寻主列表
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery searchMasterMsg) {
        return searchMasterMsgService.queryPage(searchMasterMsg);
    }

    // 接单
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto acceptDto) {
        searchMasterMsgService.accept(acceptDto);
        return AjaxResult.me().success();
    }

    @PostMapping("/reject/{msgId}")
    // 拒单
    public AjaxResult reject(@PathVariable("msgId") Long msgId, HttpServletRequest request) {
        // 获取当前登陆人信息
        Logininfo loginUser = LoginContext.getLoginUser(request);
        // 通过登录人Id找到员工信息
        Employee employee = employeeService.selectByLogininfoId(loginUser.getId());
        searchMasterMsgService.reject(msgId, employee);
        return AjaxResult.me().success();
    }

    // 从寻主池把寻主消息获取到店铺
    @PostMapping("/toSearchMasterPool/getMessage")
    public AjaxResult getMessage(@RequestBody AcceptDto acceptDto, HttpServletRequest request) {
        // 获取当前登陆人信息
        Logininfo loginUser = LoginContext.getLoginUser(request);
        // 通过登录人Id找到员工信息
        Employee employee = employeeService.selectByLogininfoId(loginUser.getId());
        searchMasterMsgService.getMessage(acceptDto, employee);
        return AjaxResult.me().success();
    }

    // 待审核-通过
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody SearchMasterMsgAuditLog searchMasterMsgAuditLog) {
        try {
            searchMasterMsgService.pass(searchMasterMsgAuditLog);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail(e.getMessage());
        }
    }
}